var activate = null;
jQ(function(){
	activate = getParameter("activate");
	if(activate==null){
		window.location.href=jQ("#ctxVal").val()+"/index.html";
	}else{
		jQ.ajax({
			type: "POST",
			url: "userAjax!resetPwdAuthorize.action",
			data:{
				activate : activate
			},
			success:function(data){
				var dataObj=eval("("+data+")");
				if(eval(dataObj["flag"])==1){
					var div_box = jQ("#restpwd").find("div");
					jQ.each(div_box,function(i,dat){
						initBulidFun(dat);
					});
				}else{
					jalert(dataObj["result"]);
					setTimeout(function(){
						window.location.href=jQ("#ctxVal").val()+"/index.html";
					},2500);
				}
			}
		});
	}
});
function checkPWD(_input,_b,fun){
	var regEx = /^[a-zA-Z0-9][a-zA-Z0-9_-]{5,19}$/;
	var _value = jQ.trim(_input.val());
	_b.removeClass().empty();
	if(!regEx.test(_value)){
		_b.addClass("wrong").html("数字,大小写字母6到30位以上组合的密码");
	}else{
		_b.addClass("right");
	}
	if(typeof(fun)=="function")
		fun();
}
function checkPWD2(_input,_b,fun){
	var pwd = _input.parent().prev("div.password").find("input").val();;
	var _value = jQ.trim(_input.val());
	_b.removeClass().empty();
	if(_value.length<=0){
		_b.addClass("wrong").html("请输入密码");
	}else if(pwd==_value){
		_b.addClass("right");
	}else{
		_b.addClass("wrong").empty().append("密码与再次重复密码不一致.");
	}
	if(typeof(fun)=="function")
		fun();
}
function initBulidFun(dat){
	var _input = jQ(dat).find("input");
	var _b = jQ(dat).find("b");
	if(jQ(dat).hasClass("password")){
		_input.blur(function(){
			checkPWD(_input,_b);
		});
	}else if(jQ(dat).hasClass("password_repeat")){
		_input.blur(function(){
			checkPWD2(_input,_b);
		});
	}else if(jQ(dat).hasClass("register_btm")){
		var _a = jQ(dat).find("input:button").click(doSubmit);
	}
}
function doSubmit(){
	var div_box = jQ("#restpwd");
	var _pwd = div_box.find("div.password");
	var _pwd2 = div_box.find("div.password_repeat");
	var _input_pwd = _pwd.find("input");
	var _input_pwd2 = _pwd2.find("input");
	var _b_pwd = _pwd.find("b");
	var _b_pwd2 = _pwd2.find("b");
	checkPWD(_input_pwd,_b_pwd,function(){
		checkPWD2(_input_pwd2,_b_pwd2,function(){
			var _flag = false;
			jQ.each(div_box.find("div"),function(i,dat){
				var _b = jQ(dat).find("b")
				if(_b.hasClass("wrong")){
					_flag = true;
				}
			})
			if(_flag){
				jalert("请输入密码");
			}else{
				jQ.ajax({
					type: "POST",
					url: "userAjax!resetPwd.action",
					data:{
						pwdInput : jQ.trim(_input_pwd.val()),
						pwdInput2 : jQ.trim(_input_pwd2.val()),
						activate : activate
					},
					success:function(data){
						var dataObj=eval("("+data+")");
						jQ("#div_msg").show();
						jQ("#restpwd").hide();
						jQ("#div_msg div.msg p").html(dataObj.result);
						var html = "";
						if(dataObj.flag==1){
							html = "<input type='button' style='width:auto' value='登录' onclick='gotoLogin()'>";
						}else{
							html = "<input type='button' style='width:auto' value='重新输入' onclick='resultInput()'>";
						}
						jQ("#div_msg div.msg_btm").html(html);
					}
				});
			}
		});
	});
}
function gotoLogin(){
	window.location.href=jQ("#ctxVal").val()+"/index.html";
}
function resultInput(){
	jQ("#div_msg").hide();
	jQ("#restpwd").show();
	jQ("#restpwd input:password").val('');
}
function getParameter(name){
    var paramStr=location.search;
    if(paramStr.length==0)return null;
    if(paramStr.charAt(0)!='?')return null;
    paramStr=decodeURIComponent(paramStr);
    paramStr=paramStr.substring(1);
    if(paramStr.length==0)return null;
    var params=paramStr.split('&');
    var p = null;
    for(var i=0;i<params.length;i++){
        if(params[i].indexOf(name) >= 0){
            p = params[i].split('=');
            p = p[1];
        }
    }
    return p;
}
function jalert(info,LFun){
	jQ("#ALERT p").text(info);
	if(LFun!=null)jQ("#ALERT").bind("LFun",LFun);
	jQ("#ALERT").dialog('open');
	jQ("#ALERT").dialog("option","buttons",{
		"确定" : function(){
			jQ(this).trigger("LFun");
			jQ(this).dialog("close");
		}
	});
}